package com.bath.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bath.Constants;
import com.bath.core.BaseController;
import com.bath.model.HandStatus;
import com.bath.model.HandToken;
import com.bath.model.User;
import com.bath.service.HandStatusService;
import com.bath.service.HandTokenService;
import com.bath.service.UserService;
import com.bath.util.StringUtil;
import com.bath.util.Util;

@Controller
@RequestMapping("/handToken.do")
public class HandTokenAction extends BaseController{
	@Autowired
	private HandTokenService handTokenService;
	@Autowired
	private HandStatusService handStatusService;
	
	
	@RequestMapping(params="method=getHandToken")
	public void getHandToken(HttpServletRequest request, HttpServletResponse response,User user) throws IOException {
		HttpSession session = request.getSession();
		
		HandToken obj = handTokenService.getById(Long.getLong(request.getParameter("id")));
		
		if(obj!=null){
			
			
			response.getWriter().print("{success:false,message:''}");
		}
		
	}
	
	@RequestMapping(params="method=getHandTokenList")
	public void getHandTokenList(HttpServletRequest request, HttpServletResponse response,String typeFlag,String handStatus) throws IOException {
		HttpSession session = request.getSession();
		StringBuffer strBuf = new StringBuffer();
		
	
		
		List<HandToken> lists ;
		if(StringUtil.isEmpty(typeFlag))typeFlag="1";
		if(StringUtil.isEmpty(handStatus))
			lists= handTokenService.getTokenAll(Short.parseShort(typeFlag));
		else
			lists= handTokenService.getList(Short.parseShort(typeFlag), Long.parseLong(handStatus));
		
		
		if(lists.size()!=0){
			strBuf.append("{success:true,tokens:[");
			//JSONArray jsonArray = JSONArray.fromObject(lists);   
			for(int i=0;i<lists.size();i++){
				HandToken obj = lists.get(i);
				if(i!=0)strBuf.append(",");
				strBuf.append("{id:'"+obj.getId()+"',");
				strBuf.append("handTokenNum:'"+obj.getHandTokenNum()+"',");
				strBuf.append("typeFlag:'"+obj.getTypeFlag()+"',");
				strBuf.append("handStatus_FK:'"+obj.getStatus().getId()+"',");
				strBuf.append("memo:'"+obj.getMemo()+"'}");
			}
		    //System.out.println(jsonArray.toString());
			strBuf.append("]}");
			
			response.getWriter().print(strBuf.toString());
		}else{
			
			response.getWriter().print("{success:false,message:''}");
		}
	}
	
	
	@RequestMapping(params="method=upHandTokenStatus")
	public void upHandTokenStatus(HttpServletRequest request, HttpServletResponse response,User user) throws IOException {
		HttpSession session = request.getSession();
		
		HandToken objToken   = handTokenService.getById(Long.getLong(request.getParameter("id")));
		HandStatus objStatus = handStatusService.getById(Long.getLong(request.getParameter("status")));
		
		if(objToken!=null){
			
			
			response.getWriter().print("{success:false,message:''}");
		}
		
	}
	
}
